import { getLocationList, addLocation, delAddress, updateAddress, getUserInfo, updateUserInfo } from '@/api/api'

export default {
    namespaced: true,
    actions: {
        // 获取收货地址
        async getLocationList(content) {
            try {
                const res = await getLocationList()
                content.commit('GETLOCATIONLIST', res.data.arr)
            } catch (e) {
                console.log(e)
            }
        },

        // 添加收货地址
        async addLocation(content, info) {
            try {
                if (info.name === '' || info.phone === '' || info.address === '') {
                    content.commit('TIP', '所输入内容不可为空')
                } else {
                    const res = await addLocation(info.name, info.phone, info.address, info.isDefaul)
                    content.commit('TIP', res.msg)
                }

            } catch (e) {
                console.log(e)
            }
        },

        // 删除收货地址
        async delAddress(content, _id) {
            try {
                const res = await delAddress(_id)
                content.commit('TIP', res.msg)
            } catch (e) {
                console.log(e)
            }
        },

        // 修改收货地址
        async updateAddress(content, item) {
            try {
                const res = await updateAddress(item._id, item.name, item.phone, item.address, item.isDefaul)
                content.commit('TIP', res.msg)
            } catch (e) {
                console.log(e)
            }
        },

        // 获取用户信息
        async getUserInfo(content) {
            try {
                const res = await getUserInfo()
                sessionStorage.setItem(
                    'user',
                    JSON.stringify(
                        {
                            username: res.data.user.username,
                            token: res.data.user.token,
                            introduce: res.data.user.introduce
                        }
                    ))
                content.commit('USERINFO', res.data.user)
                console.log(res.data.user)
            } catch (e) {
                console.log(e)
            }
        },


        // 修改用户信息
        async updateUserInfo(content, info) {
            try {
                const res = await updateUserInfo(info.username, info.sex, info.birthday, info.age, info.introduce)
                content.commit('TIP', '修改成功')
            } catch (e) {
                console.log(e)
            }
        }


    },
    mutations: {
        // 存储收货地址
        GETLOCATIONLIST(state, val) {
            state.locationList = val
        },


        // 设置提示框
        TIP(state, val) {
            state.tips.isTips = true
            state.tips.text = val
            setTimeout(() => {
                state.tips.isTips = false
                state.tips.text = ""
            }, 1000)
        },

        // 存儲用户信息
        USERINFO(state, val) {
            state.userInfo = val
        }
    },
    state: {
        locationList: [],
        tips: {
            text: "",
            isTips: false
        },
        userInfo: {}
    }
}